
#include "include/type.h"
#include "include/klib.h"


void disp_u32(u32 val, u32 info)
{
    u8 end = 9;
    char fill[11] = {'0','x','0','0','0','0','0','0','0','0','\0'};
    for (u32 i = 0; i < 8; ++i)
    {
        u8 b = (val >> (i*4)) & 0xF;
        if (b >= 10)
        {
            b -= 10;
            b += 'A';
        }
        else {
            b += '0';
        }
        fill[end-i] = b;
    }
    disp_str(fill, info);
}

void itoa(char *buff, u32 val, u32 rad) 
{
    char b[8] = {'\0'};
    u8 x;
    int idx = 0;
    int i = 0;
    if (val == 0)
    {
        if (rad == 16)
        {
            buff[0] = '0';
            buff[1] = 'x';
            i += 2;
        }
        buff[i++] = '0';
        buff[i] = '\0';
        return;
    }
    while (val != 0) {
        x = val % rad;
        val /= rad;
        if (x >= 10)
        {
            x -= 10;
            x += 'A';
        }
        else {
            x += '0';
        }
        if (idx >= 8)
        {
            break;
        }
        b[idx++] = x;
    }
    if (rad == 16)
    {
        buff[0] = '0';
        buff[1] = 'x';
        i += 2;
    }
    while (idx > 0)
    {
        buff[i++] = b[--idx];
    }
    buff[i] = '\0';
}

u32 strlen(const char *s)
{
    u32 i = 0;
    while (*s != '\0')
    {
        ++i;
        ++s;
    }
    return i;
}

void cls()
{
    memset((void*)0x0B8000, 0x0, 80*16*2);
}
